package com.acme.bankapp.server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by splash on 21.11.2014.
 */
public class BankServerTransport {

    private Socket socket;
    private static ServerSocket serverSocket;
    private DataInputStream dis;
    private DataOutputStream dos;

    public BankServerTransport(int portNumber) throws IOException {
        try {
            if (serverSocket == null) {
                serverSocket = new ServerSocket(portNumber);
            }
            System.out.println("Begin listening port " + portNumber);
            socket = serverSocket.accept();
            dis = new DataInputStream(socket.getInputStream());
            dos = new DataOutputStream(socket.getOutputStream());
            System.out.println("New user begin connection");
        } finally {
        }
    }

    public Socket getSocket() {
        return socket;
    }

    public void writeLine(String line) throws IOException {
        dos.writeUTF(line);
    }

    public String readLine() throws IOException {
        return dis.readUTF();
    }

}